package com.hongxun.budget.ui.activity.tk;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.widget.DividerItemDecoration;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DDeviceBuyEntityDao;
import com.hongxun.budget.greendao.DMaterialBuyEntityDao;
import com.hongxun.budget.greendao.DMaterialCostEntityDao;
import com.hongxun.budget.greendao.DOtherCostEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.entity.StatisticsItem;
import com.hongxun.greenentity.tk.DDeviceBuyEntity;
import com.hongxun.greenentity.tk.DMaterialBuyEntity;
import com.hongxun.greenentity.tk.DMaterialCostEntity;
import com.hongxun.greenentity.tk.DOtherCostEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class StatisticsActivity extends BaseListActivity implements RadioGroup.OnCheckedChangeListener {

    DaoSession session;
    String uuid;
    List<StatisticsItem> datas = new ArrayList<>();
    SuperAdapter<StatisticsItem> adapter;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    @Override
    public  int getMyListLayoutId() {
        return R.layout.activity_statistics;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        list.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        uuid = SPUtils.get(context, "project_guid", "").toString();
        session = GreenDaoManager.getInstance().getmDaoSession();
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup.check(R.id.radioButton1);
        setDevice();
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<StatisticsItem>(R.layout.content_statistics_item) {
            @Override
            public void bindView(ViewHolder viewHolder, StatisticsItem statisticsItem, int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(statisticsItem.getName());
                ((TextView) viewHolder.getView(R.id.count)).setText(statisticsItem.getCount() + "");
                ((TextView) viewHolder.getView(R.id.price)).setText(statisticsItem.getPrice() + "");
            }
        };
    }

    void setDevice() {
        datas.clear();
        List<String> strings = XUtils.distinctQuery(DDeviceBuyEntityDao.Properties.DBMidtype.columnName, DDeviceBuyEntityDao.TABLENAME
                , DDeviceBuyEntityDao.Properties.PGuid.columnName + " = '" + uuid + "' and " + DDeviceBuyEntityDao.Properties.DBDatastate.columnName
                        + " != '删除'");
        for (String mid : strings) {
            List<DDeviceBuyEntity> list = session.getDDeviceBuyEntityDao().queryBuilder().where(DDeviceBuyEntityDao.Properties.DBMidtype.eq(mid),
                    DDeviceBuyEntityDao.Properties.PGuid.eq(uuid), DDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (DDeviceBuyEntity d : list) {
                s.setCount(s.getCount() + d.getDBCount());
                s.setPrice(s.getPrice() + d.getDBPriceall());
            }
            datas.add(s);
        }
        adapter.notifyDataSetChanged();
    }

    void setMaterial() {
        datas.clear();
        List<String> strings = XUtils.distinctQuery(DMaterialBuyEntityDao.Properties.MBMidtype.columnName, DMaterialBuyEntityDao.TABLENAME
                , DMaterialBuyEntityDao.Properties.PGuid.columnName + " = '" + uuid + "' and " + DMaterialBuyEntityDao.Properties.MBDatastate.columnName
                        + " != '删除'");
        for (String mid : strings) {
            List<DMaterialBuyEntity> list = session.getDMaterialBuyEntityDao().queryBuilder().where(DMaterialBuyEntityDao.Properties.MBMidtype.eq(mid),
                    DMaterialBuyEntityDao.Properties.PGuid.eq(uuid), DMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (DMaterialBuyEntity d : list) {
                s.setCount(s.getCount() + d.getMBCount());
                s.setPrice(s.getPrice() + d.getMBPriceall());
            }
            datas.add(s);
        }
        adapter.notifyDataSetChanged();
    }

    void setQuantities() {
        datas.clear();
        //材料和设备的工程量
        List<String> strings = XUtils.distinctQuery(DMaterialCostEntityDao.Properties.MCQuantities.columnName, DMaterialCostEntityDao.TABLENAME
                , DMaterialCostEntityDao.Properties.PGuid.columnName + " = '" + uuid + "' and " + DMaterialCostEntityDao.Properties.MCDatastate.columnName
                        + " != '删除'");
        for (String mid : strings) {
            List<DMaterialCostEntity> list = session.getDMaterialCostEntityDao().queryBuilder().where(DMaterialCostEntityDao.Properties.MCQuantities.eq(mid),
                    DMaterialCostEntityDao.Properties.PGuid.eq(uuid), DMaterialCostEntityDao.Properties.MCDatastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (DMaterialCostEntity d : list) {
                s.setCount(s.getCount() + d.getMCCount());
                s.setPrice(s.getPrice() + d.getMCPriceall());
            }
            datas.add(s);
        }
        List<String> strings1 = XUtils.distinctQuery(DOtherCostEntityDao.Properties.OCSubclass.columnName, DOtherCostEntityDao.TABLENAME
                , DOtherCostEntityDao.Properties.PGuid.columnName + " = '" + uuid + "' and " + DOtherCostEntityDao.Properties.CCDatastate.columnName
                        + " != '删除'");
        for (String mid : strings1) {
            List<DOtherCostEntity> list = session.getDOtherCostEntityDao().queryBuilder().where(DOtherCostEntityDao.Properties.OCSubclass.eq(mid),
                    DOtherCostEntityDao.Properties.PGuid.eq(uuid), DOtherCostEntityDao.Properties.CCDatastate.notEq("删除")).list();
            StatisticsItem s = new StatisticsItem();
            s.setName(mid);
            for (DOtherCostEntity d : list) {
                s.setCount(s.getCount() + d.getOQCount());
                s.setPrice(s.getPrice() + d.getOCPriceall());
            }
            datas.add(s);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.radioButton1:
                setDevice();
                break;
            case R.id.radioButton2:
                setMaterial();
                break;
            case R.id.radioButton3:
                setQuantities();
                break;
        }
    }
}
